package com.example.okhttpdemo01.views;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.example.okhttpdemo01.R;
import com.example.okhttpdemo01.adapter.MusicAdapter;
import com.example.okhttpdemo01.base.BaseActivity;
import com.example.okhttpdemo01.contract.MusicContract;
import com.example.okhttpdemo01.entity.MusicEntity;
import com.example.okhttpdemo01.presenter.MusicPresenter;

import java.util.ArrayList;
import java.util.List;

public class MusicActivity extends BaseActivity
        implements MusicContract.IMusicView, View.OnClickListener {

    private MusicContract.IMusicPresenter mPresenter;
    private Button mButton;
    private EditText mEdit;
    private RecyclerView mRecyclerView;
    private MusicAdapter mAdapter;
    private List<MusicEntity.MusicItem> mList;

    @Override
    public int getLayoutId() {
        return R.layout.activity_music;
    }

    @Override
    public void init() {
        mPresenter = new MusicPresenter(this, this);
        mButton = findViewById(R.id.button);
        mEdit = findViewById(R.id.edit);
        mRecyclerView = findViewById(R.id.recyclerView);
        mButton.setOnClickListener(this);
        LinearLayoutManager linearLayoutManager =
                new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mList = new ArrayList<>();
        mAdapter = new MusicAdapter(mList);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button) {
            String keyWord = mEdit.getText().toString();
            if (!TextUtils.isEmpty(keyWord)) {
                mPresenter.getData(keyWord);
            }
        }
    }

    @Override
    public void showData(MusicEntity data) {
        mList = data.getData();
        mAdapter.updateData(mList);
    }
}